<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class PercentController extends BaseController{
    public function show(){
        $res=M('dict')->where(array('field'=>array('in',array('exam','project'))))->select();
        $add=0;
        foreach($res as $k=>$v){
            if($per=M('percent')->where(array('dict_id'=>$v['dict_id']))->find()){
                $perc=$per['percent'];
            }else{
                $perc=0;
            }
            $add=$add+$perc;
            $res[$k]['percent']=$perc;
        }
        $this->assign('nu',$add);
        $this->assign('res',$res);
        $this->display();
    }
    public function add(){
        $id=explode(',',I('post.id'));
        $per=explode(',',I('post.per'));
        foreach($id as $k=>$v){
            if(M('percent')->where(array('dict_id'=>$v))->find()){
                $data=[
                    'percent'=>$per[$k],
                ];
                if(M('percent')->where(array('dict_id'=>$v))->save($data)){
                    $res='保存成功';
                }else{
                    $res='保存失败';
                    $this->ajaxReturn($res);exit;
                }
            }else{
                $data=[
                    'dict_id'=>$v,
                    'percent'=>$per[$k],
                ];
                if(M('percent')->add($data)){
                    $res='保存成功';
                }else{
                    $res='保存失败';
                    $this->ajaxReturn($res);exit;
                }
            }
        }
        $this->ajaxReturn($res);
    }
}